package com.zgs.androidopengl.util

import android.content.Context
import android.renderscript.ScriptGroup.Input
import android.util.Log
import java.io.BufferedReader
import java.io.InputStreamReader
import java.util.logging.Logger

object TextResourceReader {

    fun readTextFileFromResource(context: Context, resourceId: Int): String? {

        try {
            val stringBuilder = StringBuilder()
            context.resources.openRawResource(resourceId).use { openRawResource ->
                BufferedReader(InputStreamReader(openRawResource)).use { bufferedReader ->
                    bufferedReader.useLines { lines ->
                        lines.forEach { line ->
                            stringBuilder.append(line).append("\n")
                        }
                    }
                }
            }

            return stringBuilder.toString().trim()
        } catch (e: Exception) {
            Log.e("TextResourceReader", "readTextFileFromResource: ", e)
        }
        return null
    }
}
